/** \file parserHelper.h
* Header file for helper functions of gminiParser.c
*/

#ifndef _parserHelper_h
#define _parserHelper_h

#include "gminiParser.h"
#include <map>
#include <vector>
#include <iostream>
#include <string>
#include <cstdlib>
#include "gminiGraph.h"

#ifdef DEBUG
#define out(a) cout<<a<<"\n"
#else
#define out(a) 
#endif
using namespace std;

extern TypeList temp_type; 
extern GminiGraph parsingGraph;
/**
*\brief reads input token and transforms it to an integer
*
*\return an integer value
*/
inline int getInt( ANTLR3_COMMON_TOKEN_struct* a //!<Antlr token struct
) {return atoi((char*)a->getText(a)->chars);}
/**
*\brief reads the current line in gmini code
*
*\return an integer value corresponding to a line number
*/
inline int getLine( ANTLR3_COMMON_TOKEN_struct* a //!<Antlr token struct
) {return (int)a->getLine(a);}

/**
*\brief prints gmini compile error message with the corresponding line
*
*\return a console error message
*/
inline void errorMessage(ANTLR3_COMMON_TOKEN_struct* a/*!<Antlr token struct*/,string text /*!<Error message*/)
{
	cout <<"Line "<<getLine(a)<<":"<<text<<"\n";
}
#ifdef DEBUG
/**
*\brief prints the elements in the VariableMap for debugging purposes
*
*/
void printVariables();
#endif
/**
*\brief checks if a variable is of specific Type by iterating over variables (of type VariableMap)
*
*\return true if variable is of Type t, false otherwise.
*/
bool checkBasicVariable (ANTLR3_COMMON_TOKEN_struct* a/*!<Antlr token struct*/, Type t/*!<Type to check with*/);

/**
*\brief searches for the basic type of a variable from the VariableMap at a given composite level
*
*\return a Type value
*/
Type getBasicType (string varName/*!<Variable name*/, int index/*!<Composite Level to check with*/);

/**
*\brief collects the composite Types of a variable from variables (of type VariableMap)
*
*\return a TypeList e.g. for variable of TYpe Array<Pair<int, int>>, it returns [Array, Pair, Int, Int].
*/
TypeList getCompositeType (string varName/*!<Variable Name*/);
/**
*\brief adds a new Type into Typelist
*
*/
void appendType(Type t/*!<Type to add*/);

/**
*\brief stores a new variable in  variables, the type is set to a temporary type (of type VariableMap)
*
*/
void storeVariable(string varName/*!<Variable Name*/);

/**
*\brief checks if index is either 1 or 2 for a pair variable, and displays error Message if not.
*
*\return false is index is out of bound and true if index is either 1 or 2.
*/
bool checkPairIndex( ANTLR3_COMMON_TOKEN_struct* a/*!<Antlr token struct used to extract token line*/, string index/*!<Pair index*/);

/**
*\brief collects the composite/single sub-Type(s) of a composite variable from variables (of type VariableMap) corresponding to a specified 
*index, main usage for Pair variables.
*
*\note for a Pair variable type, index can be 1 or 2. As for an Array, index can only be 1 since all elements of an array have the same
*
*\return a TypeList e.g. for variable of Type Pair<int, Array<int>> and index = 2, it returns [Array, Int].
*/
TypeList getRemainingType(string varName/*!<Variable Name*/, int index=1/*!<Composite Level to check with*/);

#endif
